package com.oilsoft.scm.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oilsoft.scm.domain.contour.Contour;
import com.oilsoft.scm.util.ContourFactory;
import com.oilsoft.scm.util.ManagerJGNUPlot;

@Service("fileProccesor")
@RemotingDestination(channels = { "my-amf" })
@Transactional
public class FileProccesorImpl implements FileProccesor {
	
	@Autowired
	ManagerJGNUPlot jgnuPlot;
	
	@Override
	@RemotingInclude
	@Deprecated
	public void process() {
		File fileContour = new File("d:/contour");
//		File filedata = new File("d:\\contour\\CONTOUR.DAT");
		Process process = null;
		try {
			String[] param = new String[2];
			param[0] = "d:\\contour\\runner.bat"; 
			param[1] = "d:\\contour\\CONTOUR.DAT";
			process = Runtime.getRuntime().exec(param, null, fileContour);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String line = null;
			while ((line = in.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	@RemotingInclude
	public byte[] doUploadPlot(byte[] bytes, String fileName) {
		try {

			File f = File.createTempFile(fileName, null);
			FileOutputStream fos = new FileOutputStream(f);
			fos.write(bytes);
			fos.close();
			byte[] conoturMap = jgnuPlot.setConoturMap(f);
			f.delete();
			return conoturMap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@Override
	@RemotingInclude
	public Contour doUpload(byte[] bytes, String fileName) {
		try {
			File f = File.createTempFile(fileName, null);
			FileOutputStream fos = new FileOutputStream(f);
			fos.write(bytes);
			fos.close();
			Contour contour = processFile(f);
			f.delete();
			return contour;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private Contour processFile(File f) {
		Process process = null;
		try {
			ClassPathResource pathResource = new ClassPathResource("executable/runner.bat");
			File dir =  pathResource.getFile().getParentFile();
			String[] param = new String[2];
			param[0] = pathResource.getFile().getPath();
			param[1] = f.getPath();
			process = Runtime.getRuntime().exec(param, null,dir);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			Contour contour = ContourFactory.createContour(in);
			process.destroy();
			return contour;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
